const Store = require('electron-store');
const {v4 : uuidv4} = require('uuid');
const path = require('path')
// const store = new Store();

// store.set('unicorn', '🦄');
// console.log(store.get('unicorn'));
// //=> '🦄'

// // Use dot-notation to access nested properties
// store.set('foo.bar', true);
// console.log(store.get('foo'));
// //=> {bar: true}

// store.delete('unicorn');
// console.log(store.get('unicorn'));
// //=> undefined

// 封装个全局存储类、

class DataStore extends Store {
    constructor(setting){
        super(setting)
        this.tracks = this.get('tracks') || []
    }
    getTracks(){
        return this.get('tracks') || []
    }
    saveTracks(){
        this.set('tracks',this.tracks)
    }
    addTracks(tracks){
        const tracsWithProps = tracks.map(track => {
            return {
                id: uuidv4(),
                path: track,
                fileName: path.basename(track)
            }
        }).filter(track => {
            const currentTracksPath = this.getTracks().map(track => track.path)
            return currentTracksPath.indexOf(track.path) < 0
        })
        this.tracks = [...this.tracks,...tracsWithProps]
        return this.saveTracks()
    }
    deleteTrack(deleteId){
        this.tracks = this.tracks.filter(item => item.id !== deleteId)
        return this.saveTracks()
    }
}

module.exports = DataStore